<template>
  <div class="language-select-container" @click.stop="toggleLanguagePanel">
    <Button icon="pi pi-globe" text rounded aria-label="Language Settings" />
    <div v-if="showLanguagePanel" class="language-panel">
      <div class="panel-header">
        <h3>{{ t('layout.languageSettings') }}</h3>
        <Button icon="pi pi-times" text rounded size="small" @click="closeLanguagePanel" />
      </div>
      <div class="panel-content">
        <div class="language-list">
          <div 
            v-for="lang in languages" 
            :key="lang.code"
            class="language-item"
            :class="{ 'active': currentLanguage === lang.code }"
            @click="changeLanguage(lang.code)"
          >
            <span class="language-flag">{{ lang.flag }}</span>
            <span class="language-name">{{ lang.name }}</span>
            <i v-if="currentLanguage === lang.code" class="pi pi-check language-check"></i>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref, computed } from 'vue'
import { useI18n } from 'vue-i18n'
import { useI18nStore } from '../../store/modules/i18n'
import Button from 'primevue/button'

const { t } = useI18n()
const i18nStore = useI18nStore()

// 语言面板显示状态
const showLanguagePanel = ref(false)

// 支持的语言列表
const languages = [
  { code: 'zh-cn', name: '简体中文', flag: '🇨🇳' },
  { code: 'en-us', name: 'English', flag: '🇺🇸' }
]

// 当前语言
const currentLanguage = computed(() => i18nStore.currentLocale)

// 切换语言面板
const toggleLanguagePanel = () => {
  showLanguagePanel.value = !showLanguagePanel.value
}

// 关闭语言面板
const closeLanguagePanel = () => {
  showLanguagePanel.value = false
}

// 切换语言
const changeLanguage = (langCode: string) => {
  i18nStore.setLocale(langCode)
  closeLanguagePanel()
}

// 点击外部关闭面板
document.addEventListener('click', () => {
  showLanguagePanel.value = false
})
</script>

<style scoped>
.language-select-container {
  position: relative;
  display: inline-block;
}

.language-panel {
  position: absolute;
  top: 100%;
  right: 0;
  background: white;
  border: 1px solid #e5e7eb;
  border-radius: 8px;
  box-shadow: 0 10px 25px rgba(0, 0, 0, 0.1);
  min-width: 200px;
  margin-top: 8px;
  z-index: 1000;
  overflow: hidden;
}

.panel-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 12px 16px;
  border-bottom: 1px solid #e5e7eb;
  background-color: #f9fafb;
}

.panel-header h3 {
  margin: 0;
  font-size: 14px;
  font-weight: 600;
  color: #374151;
}

.panel-content {
  padding: 8px 0;
}

.language-list {
  display: flex;
  flex-direction: column;
}

.language-item {
  display: flex;
  align-items: center;
  padding: 8px 16px;
  cursor: pointer;
  transition: background-color 0.2s;
  gap: 8px;
}

.language-item:hover {
  background-color: #f3f4f6;
}

.language-item.active {
  background-color: #eff6ff;
  color: #2563eb;
}

.language-flag {
  font-size: 16px;
  width: 20px;
  text-align: center;
}

.language-name {
  flex: 1;
  font-size: 14px;
  font-weight: 500;
}

.language-check {
  color: #2563eb;
  font-size: 12px;
}
</style>
